<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>4A-5A景区散点图分析</title>
    <script type="text/javascript" src="https://assets.pyecharts.org/assets/v5/echarts.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <style>
        body {
            margin: 0;
            padding: 20px;
            background-color: #f5f7fa;
            font-family: "Microsoft YaHei", sans-serif;
        }
        .chart-container {
            width: 100%;
            max-width: 1200px;
            height: 600px;
            margin: 0 auto;
            box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            background-color: #fff;
            padding: 20px;
            box-sizing: border-box;
        }
        .chart-title {
            text-align: center;
            font-size: 22px;
            color: #333;
            margin-bottom: 20px;
            font-weight: 500;
        }
    </style>
</head>
<body>
    <div class="chart-title">各省4A-5A景区数量分布</div>
    <div id="scatterChart" class="chart-container"></div>
    <script>
        $(document).ready(function() {
            // 从后端API获取数据
            $.ajax({
                url: '/menu/get_scatter_star_data',
                type: 'GET',
                dataType: 'json',
                success: function(response) {
                    if (response.code === 0) {
                        initChart(response.data);
                    } else {
                        console.error('获取数据失败:', response.msg);
                        showError('数据加载失败，请稍后重试');
                    }
                },
                error: function(xhr, status, error) {
                    console.error('请求失败:', error);
                    showError('网络错误，请检查连接后重试');
                }
            });
        });

        function initChart(chartData) {
            var chart = echarts.init(document.getElementById('scatterChart'), null, {
                renderer: 'canvas',
                locale: 'ZH'
            });

            // 按景区数量排序
            chartData.sort((a, b) => b.value - a.value);

            // 提取省份名称作为x轴数据
            var provinces = chartData.map(item => item.name);

            // 定义小清新的渐变色
            const colorList = [
                '#87CEEB', '#FFB6C1', '#FFFF99', '#FFDAB9', '#ADD8E6',
                '#FFB6C1', '#FFFF99', '#FFDAB9', '#ADD8E6', '#FFB6C1'
            ];

            var option = {
                title: {
                    text: '各省4A-5A景区数量分布',
                    subtext: '数据来源：旅游数据分析平台',
                    left: 'center',
                    textStyle: {
                        color: '#333',
                        fontSize: 22,
                        fontWeight: 'bold'
                    },
                    subtextStyle: {
                        color: '#999',
                        fontSize: 14
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: function(params) {
                        return `<div style="font-weight:bold">${params.name}</div>
                                <div>4A-5A景区数量: <span style="color:#87CEEB;font-weight:bold">${params.value[1]}</span></div>
                                <div>排名: <span style="color:#FFB6C1;font-weight:bold">${params.dataIndex + 1}</span></div>`;
                            },
                    backgroundColor: 'rgba(255,255,255,0.9)',
                    borderColor: '#ddd',
                    borderWidth: 1,
                    padding: [10, 15],
                    textStyle: {
                        color: '#333'
                    }
                },
                grid: {
                    top: 80,
                    bottom: 100,
                    left: 80,
                    right: 80,
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    data: provinces,
                    axisLabel: {
                        interval: 0,
                        rotate: 45,
                        color: '#666',
                        fontSize: 12,
                        margin: 15
                    },
                    axisLine: {
                        lineStyle: {
                            color: '#ddd'
                        }
                    },
                    axisTick: {
                        alignWithLabel: true
                    },
                    splitLine: {
                        show: true,
                        lineStyle: {
                            color: '#f0f0f0',
                            type: 'dashed'
                        }
                    }
                },
                yAxis: {
                    type: 'value',
                    name: '景区数量',
                    nameTextStyle: {
                        color: '#666',
                        padding: [0, 0, 0, 40]
                    },
                    axisLabel: {
                        color: '#666'
                    },
                    axisLine: {
                        show: true,
                        lineStyle: {
                            color: '#ddd'
                        }
                    },
                    splitLine: {
                        lineStyle: {
                            color: '#f0f0f0',
                            type: 'dashed'
                        }
                    }
                },
                visualMap: {
                    show: false,
                    min: 0,
                    max: 50,
                    dimension: 1,
                    inRange: {
                        symbolSize: [15, 60]
                    }
                },
                series: [ {
                    name: '4A-5A景区数量',
                    type: 'scatter',
                    symbolSize: function(data) {
                        return Math.sqrt(data[1]) * 8;
                    },
                    data: chartData.map((item, index) => {
                        return {
                            name: item.name,
                            value: [item.name, item.value],
                            itemStyle: {
                                color: colorList[index % colorList.length],
                                shadowBlur: 10,
                                shadowColor: 'rgba(0, 0, 0, 0.3)',
                                shadowOffsetY: 3
                            }
                        };
                    }),
                    label: {
                        show: true,
                        formatter: function(params) {
                            return params.value[1];
                        },
                        position: 'top',
                        color: '#333',
                        fontWeight: 'bold',
                        fontSize: 12
                    },
                    emphasis: {
                        label: {
                            show: true,
                            fontWeight: 'bold'
                        },
                        itemStyle: {
                            shadowBlur: 15,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    },
                    animationDelay: function (idx) {
                        return idx * 50;
                    }
                }],
                toolbox: {
                    show: true,
                    orient: 'horizontal',
                    right: 30,
                    top: 20,
                    feature: {
                        saveAsImage: {
                            show: true,
                            title: '保存为图片',
                            type: 'png',
                            pixelRatio: 2,
                            backgroundColor: 'transparent'
                        },
                        dataView: {
                            show: true,
                            title: '数据视图',
                            readOnly: true,
                            lang: ['数据视图', '关闭', '刷新']
                        },
                        restore: {
                            show: true,
                            title: '重置'
                        }
                    },
                    iconStyle: {
                        borderColor: '#666'
                    }
                },
                animationEasing: 'elasticOut',
                animationDelayUpdate: function (idx) {
                    return idx * 5;
                }
            };

            chart.setOption(option);

            // 响应式调整
            window.addEventListener('resize', function() {
                chart.resize();
            });
        }

        function showError(message) {
            $('#scatterChart').html(`<div style="text-align:center;padding-top:100px;color:#f56c6c">
                <i style="font-size:48px">⚠️</i>
                <p style="font-size:18px;margin-top:20px">${message}</p>
            </div>`);
        }
    </script>
</body>
</html>